package com.gongj.main;

import com.gongj.entity.Person;
import com.gongj.entity.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class GenericBeanDefinitionTest {
	public static void main(String[] args) {
		//容器
		DefaultListableBeanFactory ctx = new DefaultListableBeanFactory();
		//普通属性的集合  在其父类 AbstractBeanDefinition 已经提到过了
		MutablePropertyValues mpvs = new MutablePropertyValues();
		mpvs.add("id",4L);
		mpvs.add("name","gongj");
		GenericBeanDefinition parentGenericBeanDefinition = new GenericBeanDefinition();
		parentGenericBeanDefinition.setBeanClass(User.class);
		parentGenericBeanDefinition.setPropertyValues(mpvs);
		//注册到Spring容器中
		ctx.registerBeanDefinition("user",parentGenericBeanDefinition);

		GenericBeanDefinition childGenericBeanDefinition = new GenericBeanDefinition();
		//设置父BeanDefinition的名称
		childGenericBeanDefinition.setParentName("user");
		childGenericBeanDefinition.setBeanClass(Person.class);
		childGenericBeanDefinition.getPropertyValues().add("address", "上海市");
		//注册到Spring容器中
		ctx.registerBeanDefinition("person", childGenericBeanDefinition);
		User user = ctx.getBean(User.class);
		Person person = ctx.getBean(Person.class);
		System.out.println("user = " + user);
		System.out.println("person = " + person);
	}
}
